package com.pachong.android.frameworkbase.customviews;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

import com.pachong.android.frameworkbase.R;


/**
 * Drawable resizable TextView
 * <p/>
 * 可以方便控制Top/Left/Right/Bottom Drawable size
 *
 * @author Veivei Peng
 */
public class DResizableTextView extends TextView {
    private static final String TAG = DResizableTextView.class.getSimpleName();
    private BadgeView mBadgeView = null;

    private float mDrawableSizeWidth;
    private float mDrawableSizeHeight;

    public DResizableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray drawableTypeArray = getContext().obtainStyledAttributes(attrs, R.styleable.DResizableTextView);
            int drawableTopId = drawableTypeArray.getResourceId(R.styleable.DResizableTextView_android_drawableTop, -1);
            int drawableBottomId = drawableTypeArray.getResourceId(R.styleable.DResizableTextView_android_drawableBottom, -1);
            int drawableLeftId = drawableTypeArray.getResourceId(R.styleable.DResizableTextView_android_drawableLeft, -1);
            int drawableRightId = drawableTypeArray.getResourceId(R.styleable.DResizableTextView_android_drawableRight, -1);
            mDrawableSizeWidth = drawableTypeArray.getDimension(R.styleable.DResizableTextView_drawableWidth, -1.0f);
            mDrawableSizeHeight = drawableTypeArray.getDimension(R.styleable.DResizableTextView_drawableHeight, -1.0f);

            if(mDrawableSizeWidth > 0 || mDrawableSizeHeight > 0) {
                Drawable leftDrawable = getDrawable(drawableLeftId, mDrawableSizeWidth, mDrawableSizeHeight);
                Drawable topDrawable = getDrawable(drawableTopId, mDrawableSizeWidth, mDrawableSizeHeight);
                Drawable rightDrawable = getDrawable(drawableRightId, mDrawableSizeWidth, mDrawableSizeHeight);
                Drawable bottomDrawable = getDrawable(drawableBottomId, mDrawableSizeWidth, mDrawableSizeHeight);
                setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
            }
            drawableTypeArray.recycle();

        }

        mBadgeView = new BadgeView(getContext());
        mBadgeView.setTargetView(this);
    }

    private Drawable getDrawable(int drawableId, float width, float height) {
        Drawable drawable = null;
        if (drawableId > 0) {
            drawable = getDrawable(getResources().getDrawable(drawableId), width, height);
        }
        return drawable;
    }


    private Drawable getDrawable(Drawable drawable, float width, float height) {
        if (drawable != null) {
            float drawableWidth = width >= 0 ? width : drawable.getMinimumWidth();
            float drawableHeight = height >= 0 ? height : drawable.getMinimumHeight();
            drawable.setBounds(0, 0, (int) drawableWidth, (int) drawableHeight);
        }
        return drawable;
    }


    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        Drawable leftDrawable = getDrawable(left, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable topDrawable = getDrawable(top, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable rightDrawable = getDrawable(right, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable bottomDrawable = getDrawable(bottom, mDrawableSizeWidth, mDrawableSizeHeight);
        super.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
    }

    public void setCompoundDrawables(int left, int top, int right, int bottom) {
        Drawable leftDrawable = getDrawable(left, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable topDrawable = getDrawable(top, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable rightDrawable = getDrawable(right, mDrawableSizeWidth, mDrawableSizeHeight);
        Drawable bottomDrawable = getDrawable(bottom, mDrawableSizeWidth, mDrawableSizeHeight);
        super.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
    }

    public void setTextBadge(CharSequence contStr) {
        mBadgeView.setBadgeGravity(Gravity.RIGHT | Gravity.TOP);
        mBadgeView.setText(contStr);
    }

}
